#include"usermodel.hpp"
#include"CommonConnectionPool.hpp"

#include<iostream>

using namespace std;



bool UserModel::insert(User &user){
    //组装sql语句
    char sql[1024] = {0};
    sprintf(sql, "insert into User(name, password, state) values('%s', '%s', '%s')", 
            user.getName().c_str(),
            user.getPassword().c_str(),
            user.getState().c_str());
    
    //从连接池中获取一个mysql连接
    ConnectionPool* sqlConnPool = ConnectionPool::getConnectionPool();
    auto sqlConn = sqlConnPool->getConnection();

    //执行操作
    bool state = sqlConn->update(sql);
    if(state){
        user.setId(sqlConn->getInsertId());
        return true;
    }
    return false;
}

bool UserModel::queryByName(User &user){
    //组装sql语句
    char sql[1024] = {0};
    sprintf(sql, "select id, password, state from User where name = '%s'", 
            user.getName().c_str()
            );

    //从连接池中获取一个mysql连接
    ConnectionPool* sqlConnPool = ConnectionPool::getConnectionPool();
    auto sqlConn = sqlConnPool->getConnection();

    //执行查询操作
    MYSQL_RES* res = sqlConn->query(sql);
    if(res){
        MYSQL_ROW row = mysql_fetch_row(res);
        if(row != nullptr){
            //查询成功
            user.setId(stoi(row[0]));
            user.setPassword(row[1]);
            user.setState(row[2]);
            mysql_free_result(res);
            return true;
        }
    }
    mysql_free_result(res);
    return false;
}

bool UserModel::update(User &user){
    //组装sql语句
    char sql[1024] = {0};
    sprintf(sql, "update User set name = '%s', password = '%s', state = '%s' where id = %d", 
            user.getName().c_str(),
            user.getPassword().c_str(),
            user.getState().c_str(),
            user.getId()
            );

    //从连接池中获取一个mysql连接
    ConnectionPool* sqlConnPool = ConnectionPool::getConnectionPool();
    auto sqlConn = sqlConnPool->getConnection();

    //执行操作
    bool state = sqlConn->update(sql);
    if(state){
        return true;
    }
    return false;
}

bool UserModel::queryById(User &user){
    //组装sql语句
    char sql[1024] = {0};
    sprintf(sql, "select name, password, state from User where id = %d", 
            user.getId()
            );

    //从连接池中获取一个mysql连接
    ConnectionPool* sqlConnPool = ConnectionPool::getConnectionPool();
    auto sqlConn = sqlConnPool->getConnection();

    //执行查询操作
    MYSQL_RES* res = sqlConn->query(sql);
    if(res){
        MYSQL_ROW row = mysql_fetch_row(res);
        if(row != nullptr){
            //查询成功
            user.setName(row[0]);
            user.setPassword(row[1]);
            user.setState(row[2]);
            mysql_free_result(res);
            return true;
        }
    }
    mysql_free_result(res);
    return false;
}

bool UserModel::resetUserState(){
    // 组装sql语句
    char sql[1024] = {0};
    sprintf(sql, "update User set state = 'offline' where state = 'online'");
    // 从连接池中获取一个mysql连接
    ConnectionPool *sqlConnPool = ConnectionPool::getConnectionPool();
    auto sqlConn = sqlConnPool->getConnection();

    // 执行操作
    bool state = sqlConn->update(sql);
    if (state)
    {
        return true;
    }
    return false;
}